#!/usr/bin/python

import re
import subprocess
import logging

import readconfig


prefix = readconfig.readConfig("prefix")
updatedir = readconfig.readConfig("updaterdir")
app = readconfig.readConfig("app")
arg1 = readconfig.readConfig("arg1")
username = readconfig.readConfig("username")
password = readconfig.readConfig("password")
arg2 = readconfig.readConfig("arg2")
path = updatedir + app


def updateServer():
    prepStr = []
    prepStr = prefix + " " + path + " " + arg1 + " " + username + " " + password + " " + arg2
    #print prepStr

    target = subprocess.Popen(prepStr, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    strOutput = target.stdout.read()
    return strOutput

def checkIfAlreadyUpdated(strText):
    output = strText
    match = re.search("Success! App '215360' already up to date.", output)
    if match:
        logging.debug("Already Updated")
        logging.debug(match.group())
        return True
    else:
        logging.debug("Server has been updated")
        return False
